//
//  KDDayTypeController.m
//  KongDays
//
//  Created by deeplu on 12. 12. 20..
//  Copyright (c) 2012년 BlueBeans. All rights reserved.
//

#import "KDDayTypeController.h"
#import "KDNewDayController.h"
#import "KDDayType.h"

@interface KDDayTypeController ()

@end

@implementation KDDayTypeController
@synthesize dayTypeTable;

- (id)init
{
    self = [super initWithNibName:@"KDDayTypeController" bundle:nil];
    if (self) {
        self.navigationItem.backBarButtonItem.title = @"BACK";
    }
    return self;
}

- (void)dealloc
{
    [dayTypeArray release];
    [self.dayTypeTable release];
    
    [super dealloc];
}

#pragma mark - UIViewController methods

- (void)viewDidLoad
{
    [super viewDidLoad];
    
	dayTypeArray = [[KDDayType listAll] retain];
    
    self.title = NSLocalizedString(@"KDDayTypeController Title", nil);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDelegate methods

-       (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.dayTypeTable deselectRowAtIndexPath:indexPath animated:YES];
    
    NSUInteger row = indexPath.row;
    if (indexPath.section > 0)
        row++;
    
    //KDDayType *selectDayType =
    //    (KDDayType *)[dayTypeArray objectAtIndex:row];
    
    KDNewDayController *newDayController =
        [[[KDNewDayController alloc] init] autorelease];
    
    //newDayController.dayType = selectDayType;
    
    [self.navigationController pushViewController:newDayController
                                         animated:YES];
}

#pragma mark - UITableViewDataSource methods

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section;
{
    if (section == 0)
        return 1;
    else if (section == 1)
        return [dayTypeArray count] - 1;
    else
        return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell =
        [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:CellIdentifier];
        
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    NSUInteger row = indexPath.row;
    
    if (indexPath.section > 0) {
        row++;
    }
    
    KDDayType *dayType = (KDDayType *)[dayTypeArray objectAtIndex:row];
    
    cell.textLabel.text = dayType.title;
    cell.imageView.image = dayType.picture;
    
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

@end
